package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description
 * @author: ZXX-挚爱
 * @time: 2023/6/12 15:55
 */

@Service
@Slf4j
public class AddressBookServiceImpl  implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;
    @Override
    public void save(AddressBook addressBook) {
            addressBook.setUserId(BaseContext.getCurrentId());
            addressBook.setIsDefault(0);
            addressBookMapper.insert(addressBook);
    }

    @Override
    public List<AddressBook> showAddressBook(AddressBook addressBook) {
        return addressBookMapper.list(addressBook);
    }

    @Override
    public List<AddressBook> getDefaultAddress(AddressBook addressBook) {
        return addressBookMapper.list(addressBook);
    }

    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    @Override
    public AddressBook queryById(Long id) {
        return addressBookMapper.queryById(id);
    }

    @Override
    public void deleteById(Long id) {
        addressBookMapper.delete(id);
    }

    @Override
    @Transactional
    public void setDefaultAddress(AddressBook addressBook) {
        //先将该用户下面的所有地址设置非默认地址
        addressBook.setIsDefault(0);
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookMapper.updateIsDefaultByUser(addressBook);

        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);
    }


}
